<template>
  <content-card class="box-card" needFilter title="用户信息" show-reload @reload="getList">
    <template v-slot:right>
      <el-button type="text" icon="el-icon-plus" @click="handleAdd">授权用户</el-button>
      <el-button :disabled="multiple" type="text" icon="el-icon-circle-close" @click="handleDelete">取消授权</el-button>
    </template>
    <template v-slot:content>
      <el-form ref="queryForm" :inline="true" :model="queryParams" class="search-form" label-suffix="：">
        <el-form-item label="用户姓名" prop="userName">
          <el-input v-model="queryParams.userName" clearable placeholder="请输入用户姓名"
                    @keyup.enter.native="handleQuery"/>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" type="info" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <el-table v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
        <el-table-column align="center" type="selection" width="55"/>
        <el-table-column label="用户姓名" min-width="100" prop="user.name" show-overflow-tooltip/>
        <el-table-column label="用户账号" min-width="100" prop="user.code" show-overflow-tooltip/>
        <el-table-column label="手机号码" min-width="100" prop="user.mobile" show-overflow-tooltip/>
        <el-table-column label="用户类型" min-width="120" prop="user.type">
          <template slot-scope="scope">
            <dict-tag :options="localDict.user_type" :value="scope.row.user.type" show-label/>
          </template>
        </el-table-column>
        <el-table-column label="状态" min-width="80" prop="user.status">
          <template slot-scope="scope">
            <dict-tag :options="localDict.user_status" :value="scope.row.user.status"/>
          </template>
        </el-table-column>
        <el-table-column align="center" fixed="right" label="操作" width="260">
          <template slot-scope="scope">
            <el-button icon="el-icon-tickets" size="mini" type="text" @click="handleDetail(scope.row)">详情</el-button>
            <el-button icon="el-icon-circle-close" size="mini" type="text" @click="handleDelete(scope.row)">取消授权
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="pageParam.total > 0" :total="pageParam.total" :limit.sync="pageParam.pageSize"
                  :page.sync="pageParam.pageNum" :page-sizes="pageParam.options" @pagination="getList"/>
    </template>
    <el-dialog :needMax="false" :visible.sync="dialog.open" :title="dialog.title" width="800px" @confirm="handleSubmit">
      <!--      <system-user-card v-if="dialog.type === 'add'" ref="userCard" border show-search @change="handleSelect"/>-->
    </el-dialog>
  </content-card>
</template>

<script>
import {list, remove, save} from "../../../../api/base";


export default {
  name: "UserCard",
  components: {},
  props: {
    roleId: String,
  },
  data() {
    return {
      ids: [],
      single: true,
      multiple: true,
      loading: false,
      baseUrl: '/system/user/role',
      dataList: [],
      pageParam: {
        total: 0,
        pageNum: 1,
        pageSize: 10,
      },
      queryParams: {},
      localDict: {
        auth_type: [],
        user_type: [],
        auth_status: [],
        user_status: [],
      },
      dialog: {
        // 是否显示弹出层
        open: false,
        type: "",
        title: "",
      },
      formdata: {},
      formrules: {
        authType: {required: true, message: '请选择授权类型', trigger: 'blur'},
      },
    }
  },
  created() {
    this.getLocalDict()
    this.getList()
  },
  methods: {
    async getLocalDict() {
      await this.getStaticData('system-auth-type').then(res => {
        this.localDict.auth_type = res.data
      })
      await this.getStaticData('system-status').then(res => {
        this.localDict.auth_status = res.data
      })
      await this.getStaticData('system-user-identity').then(res => {
        this.localDict.user_type = res.data
      })
      await this.getStaticData('system-user-status').then(res => {
        this.localDict.user_status = res.data
      })
    },
    /** 查询角色列表 */
    getList() {
      this.loading = true;
      list(this.baseUrl, {
        ...this.queryParams,
        roleId: this.roleId,
        pageNum: this.pageParam.pageNum,
        pageSize: this.pageParam.pageSize
      }).then(res => {
        this.dataList = res.rows;
        this.pageParam.total = res.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.pageParam.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    handleAdd() {
      this.dialog.title = "授权用户"
      this.dialog.open = true
      this.dialog.type = 'add'
      this.formdata = {roleId: this.roleId, userIds: []}
    },
    handleDetail(row) {
      //详情页面是跳转到新的页面，显示信息并可以进行编辑
      this.$router.push({
        name: `SysUserDetail`,
        path: `/system/user/detail/${row.userId}`,
        params: {id: row.userId}
      })
    },
    handleDelete(row) {
      const ids = [row.id] || this.ids
      this.$modal.confirm('是否确认要取消选中的用户角色？').then(() => {
        remove(this.baseUrl, ids).then(() => {
          this.getList();
          this.$modal.msgSuccess("取消授权成功");
          this.single = true;
          this.multiple = true;
        }).catch(() => {
        })
      });
    },
    handleSelect(obj) {
      this.formdata.userIds = obj.map(item => item.id)
    },
    handleSubmit() {
      save(this.baseUrl, this.formdata).then(res => {
        console.log(res, '保存res=========>')
        this.$modal.msgSuccess('保存成功')
        this.$refs.userCard.selectedRows = []
        this.$refs.userCard.selectedKeys = []
        this.dialog.open = false
        this.getList()
      })
    },
    handleCancel() {
      this.dialog.open = false
      this.editable = false
    },
  }
}
</script>

<style scoped>

</style>
